<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :bodyStyle="bodyStyle"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:15%;height: 70%;overflow-y: hidden"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人名称">
            <a-input placeholder="请输入联系人名称" v-decorator.trim="['name', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="职位">
            <a-input placeholder="请输入职位" v-decorator.trim="['jobTitle']" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系电话">
            <a-input placeholder="请输入联系电话" v-decorator.trim="['phone', validatorRules.required]" />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addContact, checkSupplier } from '@/api/api'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'CustomerModal',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      record: null,
      isReadOnly: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.6 + 'px',
        'overflow-y': 'auto'
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入名称!' },
            { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
            { validator: this.validateSupplierName }
          ]
        },
        required: {
          rules: [{ required: true, message: '请输入!' }]
        }
      }
    }
  },
  created() {},
  methods: {
    add(record) {
      this.record = record
      this.edit()
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      console.log(this.model)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name', 'jobTitle', 'phone'))
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(this.model, values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          formData.customerId = this.record.id
          console.log('this.model.id', this.model.id)
          let obj
          if (!this.model.id) {
            obj = addContact(formData)
          } else {
            obj = addContact(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                that.$emit('ok')
              } else {
                that.$message.warning(res.data.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped></style>
